package com.grandbroad.eim.base;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.easy.west.westclient.api.login.bean.User;
import com.grandbroad.eim.utils.CacheUtils;
import com.orhanobut.hawk.Hawk;

import org.greenrobot.eventbus.EventBus;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.event.LoginStateChangeEvent;
import dev.utils.app.toast.ToastUtils;


/**
 * 所有fragment的基类
 * Created by admin on 2018/5/2.
 */

public abstract class BaseFragment extends LibFragment
{
    private Unbinder unbinder;
    private boolean isopen;
    protected ViewGroup container;
    protected AppCompatActivity activity;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activity = (AppCompatActivity) getActivity();
        isopen = setEventOpen();
        //推送相关
        JMessageClient.setNotificationFlag(JMessageClient.FLAG_NOTIFY_DEFAULT);
        //目的是为了监听登录状态
        JMessageClient.registerEventReceiver(this);
        initdata();
    }
    /**
     * 登录状态发送变化的事件
     * @param event 事件
     */
    public void onEventMainThread(LoginStateChangeEvent event){
        switch (event.getReason())
        {
            case user_deleted:
                //用户被删除
                com.grandbroad.eim.utils.ToastUtils.show("账户被删除");
                break;
            case user_logout:
               //用户退出
                break;
            case user_password_change:
               //用户密码被改
                break;
            case user_disabled:
               //用户被禁止登陆
                com.grandbroad.eim.utils.ToastUtils.show("禁止登陆");
                CacheUtils.rmoveUser();
                break;
            case user_login_status_unexpected:
                //用户登陆被挤了
                com.grandbroad.eim.utils.ToastUtils.show("该账号在其他地方被登陆");
                CacheUtils.rmoveUser();
                break;
        }
    }

    protected abstract boolean setEventOpen();


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(setlayout(),container,false);
        this.container = container;
        unbinder = ButterKnife.bind(this,v);
        initview();
        return v;
    }

    protected abstract void initdata();
    protected abstract void initview();
    //返回布局
    protected abstract int setlayout();

    @Override
    public void onStart() {
        super.onStart();
        if (isopen)
        {
            if (!EventBus.getDefault().isRegistered(this))
            {
                EventBus.getDefault().register(this);
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this))
        {
            EventBus.getDefault().unregister(this);
        }
    }

}